package com.example.iot.utils;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.iot.R;

import java.util.ConcurrentModificationException;

public class DialogUtils {
    private Context mycontext;
    public DialogUtils(Context context){
        this.mycontext=context;
    }
    private bt2ClickListener bt2ClickListener;
    public interface bt2ClickListener{
        void onbt2Click(View view);
    }

    public void setBt2ClickListener(bt2ClickListener bt2ClickListener) {
        this.bt2ClickListener = bt2ClickListener;
    }
    private DialogLoading dialogLoading;
    public interface DialogLoading{
        void Dl(Dialog dialog);
    }

    public void setDialogLoading(DialogLoading dialogLoading) {
        this.dialogLoading = dialogLoading;
    }
    private Dialog loadingdialog;
    public void initLoadingDialog(String loadingmessage){
        loadingdialog=new Dialog(mycontext);
        View view= LayoutInflater.from(mycontext).inflate(R.layout.dialog_loading,null);
        TextView text=(TextView)view.findViewById(R.id.tv_dialog_loading_text);
        text.setText(loadingmessage);
        ImageView img=(ImageView)view.findViewById(R.id.iv_dialog_loading);
        Animation animation= AnimationUtils.loadAnimation(mycontext,R.anim.whirl_anim);
        LinearInterpolator linearInterpolator=new LinearInterpolator();
        animation.setInterpolator(linearInterpolator);
        img.startAnimation(animation);
        loadingdialog.setContentView(view);
        loadingdialog.setCanceledOnTouchOutside(false);
    }
    public void showLoadingDialog(){
        loadingdialog.show();
    }
public void cancelLoadingDialog(){
        loadingdialog.dismiss();
}
    public void showNoticeDialog(String noticemessage){
        Dialog loadingdialog=new Dialog(mycontext);
        View view= LayoutInflater.from(mycontext).inflate(R.layout.dialog_notice,null);
        TextView text=(TextView)view.findViewById(R.id.tv_dialog_notice_text);
        text.setText(noticemessage);
        loadingdialog.setContentView(view);
        loadingdialog.show();
    }
    public void showDailog(String message){
        Dialog loadingdialog=new Dialog(mycontext);
        View view= LayoutInflater.from(mycontext).inflate(R.layout.dialog_main,null);
        TextView text=(TextView)view.findViewById(R.id.tv_dialog_main_message);
        text.setText(message);
        Button bt1=(Button)view.findViewById(R.id.bt_dialog_main_bt1);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadingdialog.dismiss();
            }
        });
        Button bt2=(Button)view.findViewById(R.id.bt_dialog_main_bt2);
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bt2ClickListener.onbt2Click(view);
            }
        });
        loadingdialog.setContentView(view);
        loadingdialog.show();
    }
}
